﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace theRift
{
    public class DialogScene : GameScene
    {
        protected SpriteFont smallFont;
        protected List<string> pages;
        protected string dialogLine1;
        protected string dialogLine2;
        protected string dialogLine3;
        protected int curPage;

        // Dimensions
        int width, height, top, left;

        public int CurPage
        {
            get { return curPage; }
            set { curPage = value; }
        }

        public int PageCount
        {
            get { return pages.Count; }
        }

        public DialogScene(Game game, SpriteFont smallFont)
            : base(game)
        {
            this.smallFont = smallFont;
            this.pages = new List<string>();
            this.curPage = 0;
            this.width = (int)(graphics.GraphicsDevice.Viewport.Width * 0.8);
            this.height = (int)(graphics.GraphicsDevice.Viewport.Height * 0.2);
            this.top = graphics.GraphicsDevice.Viewport.Height - height;
            this.left = (graphics.GraphicsDevice.Viewport.Width - width) / 2;
        }

        public void AddPage(string page)
        {
            pages.Add(page);
        }

        public void DialogEnd()
        {
            // We finished reading the dialog, be sure to set it
            // back to 0 for if we'd wish to read it once more!
            CurPage = 0;
        }

        public void DrawDialogLines()
        {
            string[] words = pages[curPage].Split(' ');
            dialogLine1 = null;
            dialogLine2 = null;
            dialogLine3 = null;

            for (int i = 0; i < (words.Length); i++)
            {
                if (i <= 12)
                    dialogLine1 += words[i] + " ";

                if ((i > 12) && (i <= 24))
                    dialogLine2 += words[i] + " ";

                if ((i > 24) && (i < 32))
                    dialogLine3 += words[i] + " ";
            }

            if (dialogLine1 != null)
                spriteBatch.DrawString(smallFont, dialogLine1,
                    new Vector2(left + 20, top + 20), Color.White);

            if (dialogLine2 != null)
                spriteBatch.DrawString(smallFont, dialogLine2,
                    new Vector2(left + 20, top + 20 + smallFont.LineSpacing), Color.White);

            if (dialogLine3 != null)
                spriteBatch.DrawString(smallFont, dialogLine3,
                    new Vector2(left + 20, top + 20 + (smallFont.LineSpacing * 2)), Color.White);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred,
                SaveStateMode.SaveState);
            gui.DrawDialogBox(width, height, top, left);

            DrawDialogLines();

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
